/**
 * 自己学着力扣的题解思路，实现一下
 * 题目链接：https://leetcode.cn/problems/rotate-array/solutions/551039/xuan-zhuan-shu-zu-by-leetcode-solution-nipk/
 *
 */

public class Test5 {

    public static void main(String[] args) {

        int[] nums={1,2,3,4,5};
        Solution5 solution5=new Solution5();
        solution5.rotate(nums,2);

    }

}
class Solution5 {
    //自己写一写
    public void rotate(int[] nums, int k) {
        int[] arr2 = new int[k];
        int[] arr1 = new int[nums.length-k];
        int len = nums.length;

        //int[] nums={1,2,3,4,5};

        System.arraycopy(nums, 0, arr1, 0, len-k);
        System.arraycopy(nums, len-k, arr2, 0, k);
        System.arraycopy(arr2, 0, nums, 0, k);
        System.arraycopy(arr1, 0, nums,k,len-k);

        for(int num:nums){

            System.out.println(num);

        }
    }


    //题解所给代码
    public void rotate2(int[] nums, int k) {
        int n = nums.length;
        int[] newArr = new int[n];
        for (int i = 0; i < n; ++i) {
            newArr[(i + k) % n] = nums[i];
        }
        System.arraycopy(newArr, 0, nums, 0, n);
    }
}
